package com.openvalley.cyj.parse.setter.slice;

import com.openvalley.cyj.parse.setter.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.miscservices.pasteboard.IPasteDataChangedListener;
import ohos.miscservices.pasteboard.PasteData;
import ohos.miscservices.pasteboard.SystemPasteboard;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

public class MainAbilitySlice extends AbilitySlice {
    //拿到系统的剪切板操作接口
    SystemPasteboard systemPasteboard = null;
    private static final HiLogLabel LOG_MAIN = new HiLogLabel(HiLog.LOG_APP, 0x00001, MainAbilitySlice.class.getSimpleName());
    Text text_copy = null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        text_copy = (Text)findComponentById(ResourceTable.Id_text_copy);
        text_copy.setClickedListener(listen -> doCopy());

        systemPasteboard = SystemPasteboard.getSystemPasteboard(getContext());
    }

    private void doCopy() {
        //如果存在系统剪切板操作接口
        if(systemPasteboard != null){
            //监听，但是只针对此应用程序
            systemPasteboard.addPasteDataChangedListener(new IPasteDataChangedListener() {
                @Override
                public void onChanged() {
                    HiLog.info(LOG_MAIN, "系统剪切板数据已经改变");
                }
            });
            //创建一个包含纯文本内容条目的数据对象。
            PasteData pasteData = PasteData.creatPlainTextData("abcdefg");

            //构建一个包含Intent内容条目的数据对象。
//            Intent intent = new Intent();
//            intent.setParam("action", "拨号");
//            Operation operation = new Intent.OperationBuilder()
//                    .withDeviceId("")
//                    .withUri(Uri.parse("tel:12345"))
//                    .withAction(IntentConstants.ACTION_DIAL)
//                    .build();
//            intent.setOperation(operation);
//            PasteData pasteData = PasteData.creatIntentData(intent);

            //获得数据对象上的内容条目（record）数量
//            int count = pasteData.getRecordCount();
            //获得数据对象的第一条内容条目的文本
//            String primaryText = (String) pasteData.getPrimaryText();
            //获得数据对象的第一条内容条目的类型
//            String primaryMimeType = pasteData.getPrimaryMimeType();

            //构建一个包含HTML内容条目的数据对象。
            PasteData.Record record = PasteData.Record.createHtmlTextRecord("www.baidu.com");
            pasteData.addRecord(record);

            //将数据对象（pasteData）添加到剪切板操作接口中
            systemPasteboard.setPasteData(pasteData);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}